package com.ejlchina.common.push;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

import cn.jiguang.common.ClientConfig;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.Notification;

/**
 * 
 * 极光推送管理器
 * 
 * @author Troy.Zhou
 *
 */
public class JPushManager implements PushManager, InitializingBean, DisposableBean {

	static Logger log = LoggerFactory.getLogger(JPushManager.class);
	
	
	private String appKey;
	private String masterSecret;
	private boolean testing;
	
	
	private JPushClient jpushClient = null;
	

	@Override
	public void afterPropertiesSet() throws Exception {
		ClientConfig config = ClientConfig.getInstance();
		config.setApnsProduction(!testing);
		jpushClient = new JPushClient(masterSecret, appKey, null, config);
	}

	
	@Override
	public void pushToAll(String title, String message) {
		PushPayload payload = PushPayload.newBuilder()
				.setPlatform(Platform.all())
				.setAudience(Audience.all())
				.setMessage(Message.content(message))
				.setNotification(Notification.alert(title))
				.build();
		pushPayload(payload);
	}


	
	@Override
	public void pushToUser(String title, String message, String... userId) {
		PushPayload payload = PushPayload.newBuilder()
				.setPlatform(Platform.all())
				.setAudience(Audience.alias(userId))
				.setMessage(Message.content(message))
				.setNotification(Notification.alert(title))
				.build();
		pushPayload(payload);
	}

	
	@Override
	public void pushToAndroid(String title, String message) {
		PushPayload payload = PushPayload.newBuilder()
				.setPlatform(Platform.android())
				.setAudience(Audience.all())
				.setMessage(Message.content(message))
				.setNotification(Notification.alert(title))
				.build();
		pushPayload(payload);
	}

	
	@Override
	public void pushToAndroidUser(String title, String message, String... userId) {
		PushPayload payload = PushPayload.newBuilder()
				.setPlatform(Platform.android())
				.setAudience(Audience.alias(userId))
				.setMessage(Message.content(message))
				.setNotification(Notification.alert(title))
				.build();
		pushPayload(payload);
	}

	
	@Override
	public void pushToIos(String title, String message) {
		PushPayload payload = PushPayload.newBuilder()
				.setPlatform(Platform.ios())
				.setAudience(Audience.all())
				.setMessage(Message.content(message))
				.setNotification(Notification.alert(title))
				.build();
		pushPayload(payload);
	}

	
	@Override
	public void pushToIosUser(String title, String message, String... userId) {
		PushPayload payload = PushPayload.newBuilder()
				.setPlatform(Platform.ios())
				.setAudience(Audience.alias(userId))
				.setMessage(Message.content(message))
				.setNotification(Notification.alert(title))
				.build();
		pushPayload(payload);
	}

	@Override
	public void destroy() {
		if (jpushClient != null) {
			jpushClient.close();
		}
	}

	private void pushPayload(PushPayload payload) {
		if (jpushClient == null) {
			throw new RuntimeException("Jpush Client 没有实例化！");
		}
		try {
			jpushClient.sendPush(payload);
		} catch (Exception e) {
			log.error("极光推送异常：", e);
		}
	}

	
	public void setAppKey(String appKey) {
		this.appKey = appKey;
	}

	public void setMasterSecret(String masterSecret) {
		this.masterSecret = masterSecret;
	}

	public void setTesting(boolean testing) {
		this.testing = testing;
	}

}

